﻿using AiBLSmartEdu.Module.ToDo.API.DTOs;
using AiBLSmartEdu.Module.ToDo.API.Services;
using Domain.Entities.ToDo;
using FrameworkCore;
using FrameworkCore.Controllers;
using FrameworkCore.Models;
using Microsoft.AspNetCore.Mvc;

namespace AiBLSmartEdu.Module.ToDo.API.Controllers;

/// <summary>
/// 工作待办管理
/// </summary>
public class ToDoController : BaseModuleController
{
    private readonly ToDoServices _toDoServices;

    public ToDoController(ToDoServices toDoServices)
    {
        _toDoServices = toDoServices;
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="filter"></param>
    /// <returns></returns>
    [HttpGet("Page")]
    public async Task<PageResult<ToDoItemOutputModel>> PageAsync([FromQuery] ToDoItemPaginationFilter filter)
    {
        return await _toDoServices.PageAsync(filter);
    }

    /// <summary>
    /// 用户事项类别
    /// </summary>
    /// <returns></returns>
    [HttpGet("Customer/Category")]
    public async Task<Result<List<CategoryOutputModel>>> GetCustomerToDoItemCategoryAsync()
    {
        var result = await _toDoServices.GetCustomerToDoItemCategoryAsync();
        return await Result<List<CategoryOutputModel>>.SucceedAsync(result);
    }

    /// <summary>
    /// 创建类别
    /// </summary>
    /// <param name="input">输入模型</param>
    /// <returns></returns>
    [HttpPost("Category")]
    public async Task<Result> CreateCategoryAsync(CategoryCreateInputModel input)
    {
        await _toDoServices.CreateCategoryAsync(input);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 创建事项
    /// </summary>
    /// <param name="input">输入模型</param>
    /// <returns></returns>
    [HttpPost("Item")]
    public async Task<Result> CreateToDoItemAsync(ToDoItemCreateInputModel input)
    {
        await _toDoServices.CreateToDoItemAsync(input);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改待办
    /// </summary>
    /// <param name="input">输入模型</param>
    /// <returns></returns>
    [HttpPut("Item")]
    public async Task<Result> UpdateToDoItemAsync(ToDoItemUpdateInputModel input) 
    {
        await _toDoServices.UpdateToDoItemAsync(input);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改待办事项是否完成
    /// </summary>
    /// <param name="itemId">待办事项ID</param>
    /// <param name="isCompleted">是否完成</param>
    /// <returns></returns>
    [HttpPut("Item/Completed/{itemId}/{isCompleted}")]
    public async Task<Result> UpdateToDoItemCompletedAsync(long itemId, bool isCompleted)
    {
        await _toDoServices.UpdateToDoItemCompletedAsync(itemId, isCompleted);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改待办事项等级
    /// </summary>
    /// <param name="itemId">待办事项ID</param>
    /// <param name="priorityType">事项等级</param>
    /// <returns></returns>
    [HttpPut("Item/Priority/{itemId}/{priorityType}")]
    public async Task<Result> UpdateToDoItemPriorityAsync(long itemId, ToDoPriorityType priorityType)
    {
        await _toDoServices.UpdateToDoItemPriorityAsync(itemId, priorityType);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除分类
    /// </summary>
    /// <param name="ids">唯一标识，多个使用英文状态下的逗号（,）隔开</param>
    /// <returns></returns>
    [HttpDelete("Category/{ids}")]
    public async Task<Result> DeleteCategoryAsync(string ids)
    {
        await _toDoServices.DeleteCategoryAsync(ids);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除事项
    /// </summary>
    /// <param name="ids">唯一标识，多个使用英文状态下的逗号（,）隔开</param>
    /// <returns></returns>
    [HttpDelete("Item/{ids}")]
    public async Task<Result> DeleteItemAsync(string ids)
    {
        await _toDoServices.DeleteItemAsync(ids);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除关联分类
    /// </summary>
    /// <param name="itemId"></param>
    /// <param name="categoryId"></param>
    /// <returns></returns>
    [HttpDelete("Item/{itemId}/Category/{categoryId}")]
    public async Task<Result> DeleteItemCategoryAsync(long itemId, long categoryId)
    {
        await _toDoServices.DeleteItemCategoryAsync(itemId, categoryId);
        return await Result.SucceedAsync();
    }
}
